<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<body>
		<form>
			<table width="100%">
				<tr>
					<td align="left">
						<label>
							Instrument type:
						</label>
					</td>
					<td align="right">
						<input dojoType="dijit.form.TextBox" id="searchType" /> 
					</td>
				</tr>
				<tr>
					<td align="left">
						<label>
							Instrument description:
						</label>
					</td>
					<td align="right">
						<input dojoType="dijit.form.TextBox" id="searchDescription" />
					</td>
				</tr>
				<tr>
					<td align="left">
						<label>
							Price (upper bound):
						</label>
					</td>
					<td align="right">
						<input dojoType="dijit.form.TextBox" id="searchPrice" />
					</td>
				</tr>
				<tr>
					<td colspan=2 align="right">
						<button dojoType="dijit.form.Button">
							Cancel
							<script event="onClick" args="event" type="dojo/method">
								dijit.byId( "instrumentSearch").destroyRecursive( );
							</script>
						</button>
						<button dojoType="dijit.form.Button">
							Search
							<script event="onClick" args="event" type="dojo/method">
								dojo.xhrPost( {
									handleAs : "json",
									url : "/FinalProject/app/instrumentManagement",
									content : {
										instrumentOp : "instrument_extended_search",
										type		 : dijit.byId( "searchType").getValue( ),
										description	 : dijit.byId( "searchDescription").getValue( ),
										usagePrice	 : dijit.byId( "searchPrice").getValue( )
									},
									load : function( data, ioArgs) {
										if ( data == "ERROR") {
											dijit.byId( "toaster").setContent( "Error while searching instrument. Try again.", "fatal");
											dijit.byId( "toaster").show( );
											return;
										}
										var resultsDialog = new dijit.Dialog( {
											title 	: "Search Results",
											href 	: "/FinalProject/templates/instruments/searchResult.jsp",
											id		: "searchResultDialog",
											style	: "width:800px;height:400px",
											onCancel: function( event) {
												this.destroyRecursive( );
											}
										});
										dojo.connect( resultsDialog, "onLoad", function( event) {
											dijit.byId( "instrumentSearch").destroyRecursive( );
											var newData = {
												identifier : "id",
												items : data.items
											};
											var resultsStore = new dojo.data.ItemFileWriteStore( { data: newData, clearOnClose: true, jsId:"resultsStore"});
											dijit.byId( "searchResultGrid").setStore( resultsStore);
										});
										resultsDialog.show( );
									},
									error : function( ) {
										dijit.byId( "toaster").setContent( "Backend error while searching instrument. Try again.", "fatal");
										dijit.byId( "toaster").show( );
										return;
									}
								});
							</script>
						</button>
					</td>
				</tr>
			</table>
		</form>	
	</body>
</html>